/*
 * CS_REQ_WORLD_BOSS_CREATE_SINGLE.cpp
 *
 *  Created on: 2014. 1. 8.
 *      Author: jjaehuny
 */

#include "../../Command.h"

#include "../../User.h"
#include "../../NLogic/Static.h"
#include "../../NLogic/Space.h"
#include "../../NLogic/Stamina.h"
#include "../../NResource/Static.h"

#include "Common.h"

namespace ProjectZ { namespace NCommand {

static void SendAck_SC_REQ_WORLD_BOSS_CREATE_SINGLE(User * pUser, int16_t ack)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ack);
	pAck->MakePacket(CMD_SC_REQ_WORLD_BOSS_CREATE_SINGLE);
	pUser->Send(pAck);
}

void Command::CS_REQ_WORLD_BOSS_CREATE_SINGLE(User * pUser, ::Network::Packet * pPacket)
{
	/// 솔로 플레이
	/*
	CS_REQ_WORLD_BOSS_CREATE_SINGLE			0x400C
	{

	}

	SC_REQ_WORLD_BOSS_CREATE_SINGLE			0x400D
	{
		s16	ack;				/// 1:완료 ,  ACK_BATTLE_AFTEREFFECT = 1400,	전투 후유증... 중...
	}
	*/

	NLogic::Space * pWorld = pUser->GetSpace();
	if (NULL == pWorld)
	{
		DEBUGLOG("%s NULL == pWorld", __FUNCTION__);
		SendAck_SC_REQ_WORLD_BOSS_CREATE_SINGLE(pUser, ACK_NO_SPACE);
		return;
	}

	if (false == pUser->EndBattle_afterEffect())
	{
		DEBUGLOG("%s false == pUser->EndBattle_afterEffect()", __FUNCTION__);
		SendAck_SC_REQ_WORLD_BOSS_CREATE_SINGLE(pUser, ACK_BATTLE_AFTEREFFECT);
		return;
	}

	if (pWorld->GetData()._spaceType != NLogic::Space::SpaceType::WORLD)
	{
		DEBUGLOG("%s pWorld->GetData()._spaceType != NLogic::Space::SpaceType::WORLD", __FUNCTION__);
		SendAck_SC_REQ_WORLD_BOSS_CREATE_SINGLE(pUser, ACK_NO_SPACE);
		return;
	}

	if (pUser->GetEnumState() != User::EnumState::__NONE__)
	{
		DEBUGLOG("%s pUser->GetEnumState() != User::EnumState::__NONE__", __FUNCTION__);
		SendAck_SC_REQ_WORLD_BOSS_CREATE_SINGLE(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NResource::EventTimeResource * pEventTimeResource = NResource::Static::Instance().GetEventTime();
	if (NULL == pEventTimeResource)
	{
		DEBUGLOG("%s NULL == pEventTimeResource", __FUNCTION__);
		SendAck_SC_REQ_WORLD_BOSS_CREATE_SINGLE(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	if (false == pEventTimeResource->IsWorldBossEvent(pUser))
	{
		DEBUGLOG("%s false == pEventTimeResource->IsWorldBossEvent(pUser)", __FUNCTION__);
		SendAck_SC_REQ_WORLD_BOSS_CREATE_SINGLE(pUser, ACK_WORDBOSS_NOT_EVENT);
		return;
	}

	NResource::WorldBoss * pWorldBossResource = NResource::Static::Instance().GetWorldBossResource();
	if (NULL == pWorldBossResource)
	{
		DEBUGLOG("%s NULL == pWorldBossResource", __FUNCTION__);
		SendAck_SC_REQ_WORLD_BOSS_CREATE_SINGLE(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NLogic::Stamina * pStamina = pUser->GetStamina();
	if (NULL == pStamina)
	{
		DEBUGLOG("%s NULL == pStamina", __FUNCTION__);
		SendAck_SC_REQ_WORLD_BOSS_CREATE_SINGLE(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	if (true == pStamina->IsKeyLack(1))
	{
		DEBUGLOG("%s staminaGap < 0", __FUNCTION__);
		SendAck_SC_REQ_WORLD_BOSS_CREATE_SINGLE(pUser, ACK_NOT_ENOUGHT_STAMINA);
		return;
	}

/*	time_t currentSec;
	time(&currentSec);

	// 현재 스태미너가 최대라면 남은 회복 시간을 현재시간으로 설정
	if (pUser->GetCharacterInfo().event_stamina == STAMINA_MAX)
		pUser->GetCharacterInfo().recover_stamina_sec = currentSec;

	int staminaGap = pUser->GetCharacterInfo().event_stamina - 1;

	// 보유 스태미너가 부족한 경우
	if (staminaGap < 0)
	{
		DEBUGLOG("%s staminaGap < 0", __FUNCTION__);
		SendAck_SC_REQ_WORLD_BOSS_CREATE_SINGLE(pUser, ACK_NOT_ENOUGHT_STAMINA);
		return;
	}*/

	pWorld->LeaveUser(pUser);

	NLogic::Space::Data clsData;
	clsData._maxUser = 1;
	clsData._table_id = pWorldBossResource->GetData().u16map_id;
	clsData._difficulty = 0;
	clsData._spaceType = NLogic::Space::SpaceType::WORLDBOSS_SINGLE;
	clsData._boss_tid = pWorldBossResource->GetData().u16boss_tid1;

	NLogic::Space * pSingleDungeon = NLogic::Static::Instance().GETSPACEPOOL()->NEW();
	pSingleDungeon->Open(pUser, clsData);

	pSingleDungeon->EnterUser(pUser);

	pUser->SetEnumState(User::EnumState::WORLDBOSS_SINGLE);

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ACK_OK);
	pAck->MakePacket(CMD_SC_REQ_WORLD_BOSS_CREATE_SINGLE);
	pUser->Send(pAck);
	STRESSLOG("CMD_SC_REQ_WORLD_BOSS_CREATE_SINGLE ACK");

	pStamina->DecreaseKey(1);
//	pUser->GetCharacterInfo().event_stamina -= 1;


	SendWorldBossDetailInfo(pUser);
}

} /*NCommand*/ } /*ProjectZ*/


